package com.ten.mall.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ten.mall.mapper.PrivilegeMapper;
import com.ten.mall.service.PrivilegeService;
import com.ten.mall.vo.Privilege;
import com.ten.mall.vo.PrivilegeExample;
import com.ten.mall.vo.PrivilegeExample.Criteria;
import com.ten.mall.vo.PrivilegeUser;

@Service
@Transactional
public class PrivilegeServiceImpl implements PrivilegeService {
	@Autowired
	private PrivilegeMapper pMapper;
	@Override
	@Transactional(readOnly=true)
	public Map<String,Object> selectByPages(Integer start, Integer size, String name) {
		Map<String,Object> map =new HashMap<String, Object>();
		PageHelper.startPage(start, size);
		PrivilegeExample example = new PrivilegeExample();
		Criteria c =example.createCriteria();
		if(StringUtils.isNotBlank(name)){
			c.andQuanNameLike("%"+name+"%");
		}
		example.setOrderByClause("quan_order");
		List<Privilege> list =pMapper.selectByExample(example);
		PageInfo<Privilege> info =new PageInfo<>(list);
		map.put("total", info.getTotal());
		map.put("list", list);
		return map;
	}

	@Override
	public int insertPrivilege(Privilege add) {
		int i =pMapper.insert(add);
		return i;
	}

	@Override
	public int updatePrivilege(Privilege add) {
		int i =pMapper.updateByPrimaryKeySelective(add);
		return i;
	}

	@Override
	public int deletePrivilege(Integer id) {
		int i =pMapper.deleteByPrimaryKey(id);
		return i;
	}

	@Override
	@Transactional(readOnly=true)
	public Privilege selectById(Integer id) {
		Privilege  p =pMapper.selectByPrimaryKey(id);
		return p;
	}

	@Override
	@Transactional(readOnly=true)
	public List<Privilege> selectAll() {
		PrivilegeExample example = new PrivilegeExample();
		List<Privilege> list =pMapper.selectByExample(example);
		return list;
	}

	@Override
	@Transactional(readOnly=true)
	public List<PrivilegeUser> selectMyPrilige(String id) {
		List<PrivilegeUser> list =pMapper.selectMyPrilige(id);
		return list;
	}

	@Override
	@Transactional(readOnly=true)
	public List<Privilege> selectMyRole(Integer roleId) {
		List<Privilege> list =pMapper.selectMyRole(roleId);
		return list;
	}

	@Override
	public int deletMyRole(Integer roleId) {
		int i =pMapper.deletMyRole(roleId);
		return i;
	}

}
